<template>
  <div class="habits-container">
    <div class="habits-header">
      <h2>习惯管理</h2>
      <el-button type="primary" @click="showAddDialog">添加习惯</el-button>
    </div>

    <el-table :data="habits" style="width: 100%">
      <el-table-column prop="habitName" label="习惯名称" />
      <el-table-column prop="habitContent" label="习惯内容" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="text" @click="editHabit(scope.row)">编辑</el-button>
          <el-button type="text" class="delete-btn" @click="deleteHabit(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form :model="habitForm" label-width="80px">
        <el-form-item label="习惯名称">
          <el-input v-model="habitForm.habitName" />
        </el-form-item>
        <el-form-item label="习惯内容">
          <el-input
            type="textarea"
            v-model="habitForm.habitContent"
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveHabit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getHabits, createHabit, updateHabit } from '@/api/habits'
import { ElMessage, ElMessageBox } from 'element-plus'

const habits = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('')
const habitForm = ref({
  habitName: '',
  habitContent: '',
  createId: 1 // 这里暂时hardcode用户ID，实际应该从登录用户中获取
})
const isEdit = ref(false)
const currentHabitId = ref(null)

// 获取习惯列表
const fetchHabits = async () => {
  try {
    const response = await getHabits(1) // 用户ID hardcode为1
    habits.value = response.data
  } catch (error) {
    console.error('获取习惯列表失败:', error)
    ElMessage.error('获取习惯列表失败')
  }
}

// 显示添加对话框
const showAddDialog = () => {
  dialogTitle.value = '添加习惯'
  habitForm.value = {
    habitName: '',
    habitContent: '',
    createId: 1
  }
  isEdit.value = false
  dialogVisible.value = true
}

// 编辑习惯
const editHabit = (habit) => {
  dialogTitle.value = '编辑习惯'
  habitForm.value = { ...habit }
  currentHabitId.value = habit.id
  isEdit.value = true
  dialogVisible.value = true
}

// 保存习惯
const saveHabit = async () => {
  try {
    if (isEdit.value) {
      await updateHabit(currentHabitId.value, habitForm.value)
      ElMessage.success('习惯更新成功')
    } else {
      await createHabit(habitForm.value)
      ElMessage.success('习惯创建成功')
    }
    dialogVisible.value = false
    fetchHabits()
  } catch (error) {
    console.error('保存习惯失败:', error)
    ElMessage.error('保存习惯失败')
  }
}

// 删除习惯
const deleteHabit = async (id) => {
  try {
    await ElMessageBox.confirm('确定要删除这个习惯吗？', '提示', {
      type: 'warning'
    })
    await deleteHabit(id)
    ElMessage.success('习惯删除成功')
    fetchHabits()
  } catch (error) {
    if (error !== 'cancel') {
      console.error('删除习惯失败:', error)
      ElMessage.error('删除习惯失败')
    }
  }
}

onMounted(() => {
  fetchHabits()
})
</script>

<style scoped>
.habits-container {
  padding: 20px;
}

.habits-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.delete-btn {
  color: #F56C6C;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 